package com.torry.replay.domain;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.torry.replay.api.result.DashboardStatResult;
import com.torry.replay.infras.entity.DashboardDO;
import com.torry.replay.infras.repo.DashboardRepository;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DashBoardService {

    @Resource
    private DashboardRepository dashboardRepository;

    public DashboardStatResult getDashboardStatInfo(Integer type, Integer radius) {
        LocalDate now = LocalDate.now();
        LocalDate startDate = radius == 1 ? now.minusYears(1) : radius == 2 ? now.minusMonths(3) : now.minusMonths(1);
        List<DashboardDO> list = dashboardRepository.list(new LambdaQueryWrapper<DashboardDO>().ge(DashboardDO::getDate, startDate).le(DashboardDO::getDate, now).orderByAsc(DashboardDO::getDate));
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        List<String> dates = list.stream().map(e -> e.getDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))).collect(Collectors.toList());

        DashboardStatResult result = new DashboardStatResult();
        result.setDates(dates);

        if (type == 1) {
            List<Integer> riseNums = list.stream().map(DashboardDO::getRiseNum).collect(Collectors.toList());
            List<Integer> failNums = list.stream().map(DashboardDO::getFailNum).collect(Collectors.toList());
            List<Integer> flatNums = list.stream().map(DashboardDO::getFlatNum).collect(Collectors.toList());
            result.setRiseNums(riseNums);
            result.setFailNums(failNums);
            result.setFlatNums(flatNums);
        } else if (type == 2) {
            List<Integer> limitUps = list.stream().map(DashboardDO::getLimitUpNum).collect(Collectors.toList());
            List<Integer> limitDowns = list.stream().map(DashboardDO::getLimitDownNum).collect(Collectors.toList());
            result.setLimitUpNums(limitUps);
            result.setLimitDownNums(limitDowns);
        } else if (type == 3) {
            List<Double> foreignCapitals = list.stream().map(e -> e.getForeignTrend() == 2 ? -e.getForeignValue() : e.getForeignValue()).collect(Collectors.toList());
            List<Double> mainCapitals = list.stream().map(e -> e.getMainTrend() == 2 ? -e.getMainValue() : e.getMainValue()).collect(Collectors.toList());
            result.setForeignCapitals(foreignCapitals);
            result.setMainCapitals(mainCapitals);
        } else if (type == 4) {
            List<Double> totalVolumes = list.stream().map(DashboardDO::getTotalVolume).collect(Collectors.toList());
            result.setVolumes(totalVolumes);
        }
        return result;
    }

    public static void main(String[] args) {
        List<String> list = Arrays.asList("0", "1", "2", "3", "4", "5", "6", "7", "8");
        for (int i = 0; i < list.size(); i += 10) {
            if (i + 10 >= list.size()) {
                System.out.println(list.subList(i, list.size()));
                break;
            }
            System.out.println(list.subList(i, i + 10));
        }
    }
}
